﻿using EasyCore.Authentications.Dtos;
using EasyCore.Authentications;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using EasyAdmin.Application.Contracts.Systems.Auth.Dtos;
using EasyAdmin.Application.Systems;
using NPOI.Util;
using EasyAdmin.Application.Contracts.Systems.Auth;

namespace EasyAdmin.Web.Host.Controllers
{
    /// <summary>
    /// 登录管理
    /// </summary>
    [Route("api/[controller]",Name ="登录管理")]
    [ApiController]
    ////[ApiExplorerSettings(GroupName ="登录管理")]
    public class AccountController : ControllerBase
    {
        private readonly IAuthTokenService _authTokenService;
        private readonly IAuthAppService _authAppService;

        public AccountController(IAuthTokenService authTokenService, IAuthAppService authAppService)
        {
            _authTokenService = authTokenService;
            _authAppService = authAppService;
        }

        /// <summary>
        /// 获得验证码
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("captcha",Name ="获得验证码")]
        public async Task<dynamic> GetCaptchaInfo()
        {
            return await _authAppService.GetCaptchaInfo();
        }

        /// <summary>
        /// 登录认证
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("login")]
        
        public async Task<dynamic> Login([FromBody] LoginInput dto)
        {
            var result = await  _authAppService.Login(dto);
            return result;
        }

        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("refresh-token")]
        public async Task<dynamic> RefreshToken([FromBody] AuthTokenDto dto)
        {
            var token = await _authTokenService.RefreshAuthTokenAsync(dto);
            return token;
        }


        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task LoginOut() {
            await _authAppService.LoginOut();
        }
    }
}
